import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_boost/boost_navigator.dart';
import 'package:flutter_module/common/base/base_page.dart';
import 'package:flutter_module/common/widget/CustomDialog.dart';
import 'package:flutter_module/page/base/base_view.dart';
import 'package:flutter_module/page/base/max_input_edit_text_page.dart';
import 'package:flutter_module/style/colors.dart';

/// 合作服务商优惠政策
class ServiceProviderEditFavouredPolicyPage extends BaseStateFulWidget {
  ServiceProviderEditFavouredPolicyPage({required this.params});

  final Map params;

  @override
  BaseState<StatefulWidget> buildState() {
    return _ServiceProviderEditFavouredPolicyState();
  }
}

class _ServiceProviderEditFavouredPolicyState
    extends BaseState<ServiceProviderEditFavouredPolicyPage> with BasePage {
  String? preferential;
  final String keyFavouredPolicy = "preferential";

  @override
  void initState() {
    preferential = widget.params[keyFavouredPolicy] ?? "";
    super.initState();
  }

  @override
  String get appBarTitle => "优惠政策";

  bool saveClick = false;

  @override
  getActions() {
    return [
      new TextClickView(
        title: "保存",
        isTitleRight: true,
        rightClick: () {
          _onSubmit();
        },
      )
    ];
  }

  var key = GlobalKey<MaxInputEditTextPageState>();

  @override
  Widget createView(
    BuildContext context,
  ) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 16, right: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            child: Text(
              "请填写您的优惠政策，为空则不展示",
              style: TextStyle(color: ThemeColors.text333, fontSize: 12),
            ),
            padding: const EdgeInsets.only(bottom: 14.0, top: 14.0),
          ),
          MaxInputEditTextPage(
            key: key,
            content: preferential ?? "",
            hintText: "输入您的优惠条款",
            maxLength: 120,
            showLine: 5,
          )
        ],
      ),
    );
  }

  @override
  onBack() async {
    if (saveClick) {
      return true;
    } else {
      showDialog(
          context: context,
          barrierDismissible: false,
          builder: (_) {
            return CustomDialog(
              content: '保留此次编辑',
              cancelContent: "不保留",
              confirmContent: "保留",
              dismissCallback: () async {
                await BoostNavigator.instance.pop();
                super.onBack();
              },
              confirmCallback: () async {
                await BoostNavigator.instance.pop();
                _onSubmit();
              },
            );
          },
          useRootNavigator: false);
    }
    // return true;
  }

  _onSubmit() {
    saveClick = true;
    Map<String, Object> result = {keyFavouredPolicy: key.currentState!.text};
    BoostNavigator.instance.pop(result);
  }
}
